/**
 * 用户相关Store
 */
import { makeAutoObservable } from "mobx";
import {
  fetchRegisterUser,
  fetchLoginUser,
  fetchAllUser,
  fetchLogout,
  fetchChangePermission,
  fetchCheckUser,
  fetchChangePassword
} from "../api/user";
import { checkResonse } from "../api";
import Crypoto from "../utils/Crypoto";
import { saveloginUser } from "../utils/login";
import { ITUserInfo } from "./interfaces";

interface TLoginparam {
  account: string;
  password: string;
  email?: string;
}

interface ITBadResp {
  msg: string;
}

export interface ITUserStore {
  // 获取当前用户信息
  getUserInfo: () => ITUserInfo;

  // 注册用户loading状态
  registerLoading: boolean;

  // 修改注册用户loading状态
  changeRegisterLoading: (data: boolean) => void;

  // 注册用户请求
  registerUser: (data: TLoginparam) => Promise<ITBadResp | null>;

  // 用户登录loading状态
  loginLoading: boolean;

  // 修改用户登录状态
  changeLoginLoading: (data: boolean) => void;

  // 登录用户请求
  loginUser: (data: TLoginparam) => Promise<ITBadResp | null>;

  // 所有用户
  allUser: ITUserInfo[];

  // 更新所有用户
  changeAllUser: (data: ITUserInfo[]) => void;

  // 所有用户请求状态
  allUserLoading: boolean;

  // 修改所有用户请求状态
  changeAllUserLoading: (data: boolean) => void;

  // 请求所有用户数据
  getAllUser: () => Promise<void>;

  // 注销
  logUserOut: () => Promise<ITBadResp | null>;

  // 修改权限
  changePermission: (data: any) => Promise<ITBadResp | null>;

  // 校验账号和邮箱
  checkAccount: (data: any) => Promise<ITBadResp | null>;

  // 修改密码
  changePassword: (data: any) => Promise<ITBadResp | null>;
}

class UserStore {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true });
  }

  // 获取当前用户信息
  getUserInfo = (): ITUserInfo => {
    let userInfo = null;
    try {
      let userstring = sessionStorage.getItem("USERINFO") || "";
      userInfo = JSON.parse(userstring);
    } catch (e) {}
    return userInfo;
  };

  // 注册用户loading状态
  registerLoading = false;

  // 修改注册用户loading状态
  changeRegisterLoading = (data: boolean) => (this.registerLoading = data);

  // 注册用户请求
  registerUser = async (data: TLoginparam): Promise<ITBadResp | any> => {
    const { account, password, email } = data;
    const cry: any = new Crypoto();
    const encryptStr = cry.encrypt(password);
    this.changeRegisterLoading(true);
    const res = await fetchRegisterUser({
      account,
      password: encryptStr,
      email,
    });
    this.changeRegisterLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 用户登录loading状态
  loginLoading = false;

  // 修改用户登录状态
  changeLoginLoading = (data: boolean) => (this.loginLoading = data);

  // 登录用户请求
  loginUser = async (data: TLoginparam): Promise<ITBadResp | any> => {
    const { account, password } = data;
    const cry: any = new Crypoto();
    const encryptStr = cry.encrypt(password);
    this.changeLoginLoading(true);
    const res = await fetchLoginUser({ account, password: encryptStr });
    this.changeLoginLoading(false);
    if (checkResonse(res)) {
      const { data } = res.data;
      saveloginUser(data);
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 所有用户
  allUser: ITUserInfo[] = [];

  // 更新所有用户
  changeAllUser = (data: ITUserInfo[]) => (this.allUser = data);

  // 所有用户请求状态
  allUserLoading = false;

  // 修改所有用户请求状态
  changeAllUserLoading = (data: boolean) => (this.allUserLoading = data);

  // 请求所有用户数据
  getAllUser = async (): Promise<void> => {
    this.changeAllUserLoading(true);
    const res = await fetchAllUser();
    this.changeAllUserLoading(false);
    if (checkResonse(res)) {
      const { data } = res.data;
      let temp: ITUserInfo[] = [];
      data.forEach((item: ITUserInfo, index: number) => {
        temp.push({
          ...item,
          key: index,
        });
      });
      this.changeAllUser(temp);
    }
  };

  // 注销
  logUserOut = async (): Promise<ITBadResp | null> => {
    const res = await fetchLogout(this.getUserInfo());
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 修改权限
  changePermission = async (data: any): Promise<ITBadResp | null> => {
    const res = await fetchChangePermission(data);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  checkAccount = async (data: any): Promise<ITBadResp | null> => {
    const res = await fetchCheckUser(data);
    if (res && checkResonse(res)) {
      return res.data.data;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 修改密码
  changePassword = async (data: any): Promise<ITBadResp | null> => {
    const { id, password } = data;
    const cry: any = new Crypoto();
    const encryptStr = cry.encrypt(password);

    const res = await fetchChangePassword({ id, password: encryptStr });
    if (res && checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };
}

const userStore: ITUserStore = new UserStore();
export default userStore;
